package com.xinlong.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinlong.dao.Iproductdao;
import com.xinlong.dao.MemberMapper;
import com.xinlong.dao.OrdersDao;
import com.xinlong.dao.TravellerMapper;
import com.xinlong.entity.Member;
import com.xinlong.entity.Orders;
import com.xinlong.entity.Traveller;
import com.xinlong.entity.TravellerExample;
import com.xinlong.service.Ordersservice;
import com.xinlong.utils.IDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class Ordersserviceimpl implements Ordersservice {

    @Autowired
    OrdersDao dao;
    @Autowired
    private TravellerMapper travellerMapper;
    @Autowired
    private Iproductdao iproductdao;
    @Autowired
    private MemberMapper memberMapper;


    public List<Orders> findAll(int page,int size,String orderNum,String orderStatus) throws Exception {
        PageHelper.startPage(page,size);
        return dao.selByCondition(orderNum,orderStatus);
    }
    public Orders findbyidAll(String id) {
        Orders orders =  dao.findbyid(id);
        List<Traveller> travellers = orders.getTravellers();
        for(Traveller temp:travellers){
            if(temp.getSex().equals("0")){
                temp.setSex("男");
            }else{
                temp.setSex("女");
            }
            if(temp.getCredentialsType()==1){
                temp.setCredentialsTypeStr("身份证");
            }
        }
        return orders;
    }

    @Override
    public int saveOrder(List<Traveller> travellers, Member member, String productId,String orderDesc,String routeId) throws Exception {
        Orders orders = new Orders();
        orders.setPrice(Double.parseDouble(travellers.size()*iproductdao.selProductPrice(Integer.parseInt(productId))+""));
        int index = 0;
        member.setId((IDUtils.genItemId()+"").substring(8));
        System.out.println(member.getId());
        index = memberMapper.insert(member);
        if(index!=0){
            for(Traveller traveller:travellers){
                if(traveller.getSex().equals("女")){
                    traveller.setSex("1");
                }else{
                    traveller.setSex("0");
                }

                if(traveller.getCredentialsTypeStr().equals("身份证")){
                    traveller.setCredentialsType(1);
                }
                traveller.setId((IDUtils.genItemId()+"").substring(8));
                traveller.setMemberId(Integer.parseInt(member.getId()));
                index += travellerMapper.insertSelective(traveller);
            }
            if(index-1!=travellers.size()){
                throw  new Exception("添加失败");
            }
        }

        orders.setMemberId(Integer.parseInt(member.getId()));
        orders.setOrderNum((IDUtils.genItemId()+"").substring(8));
        orders.setOrderDesc(orderDesc);
        orders.setOrderTime(new Date());
        orders.setPayType(1);
        orders.setProductId(Integer.parseInt(productId));
        orders.setOrderStatus(1);
        orders.setRouteId(Integer.parseInt(routeId));
        index =  dao.insOrder(orders);
        return index;
    }

    @Override
    public int update(Member member,List<Traveller> travellers,String productId,String orderDesc,String routeId,String orderNum) throws Exception {
        int i = 0;
        TravellerExample example = new TravellerExample();
        example.createCriteria().andMemberidEqualTo(Integer.parseInt(member.getId()));
        travellerMapper.deleteByExample(example);

        for(Traveller traveller:travellers){
            if(traveller.getSex().equals("女")){
                traveller.setSex("1");
            }else{
                traveller.setSex("0");
            }
            traveller.setMemberId(Integer.parseInt(member.getId()));
            if(traveller.getCredentialsTypeStr().equals("身份证")){
                traveller.setCredentialsType(1);
            }
            traveller.setId(null);
            i += travellerMapper.insert(traveller);
        }
        if(i!=travellers.size()){
            throw new Exception("修改失败");
        }
        i = memberMapper.updateByPrimaryKeySelective(member);
        Orders orders = new Orders();
        orders.setOrderNum(orderNum);
        orders.setOrderDesc(orderDesc);
        orders.setProductId(Integer.parseInt(productId));
        orders.setRouteId(Integer.parseInt(routeId));
        i = dao.update(orders);
        return  i;
    }

    @Override
    public void updateStatus() {
        dao.updateStatus2();
        dao.updateStatus3();

    }

}
